﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;

namespace CoachingOrganizer.Android.Adapters.Tools.MoneyCalculation
{
    public class ReportPeriodsAdapter : BaseAdapter
    {
        private readonly Activity _context;
        private readonly MoneyCalculationModel _model;
        private List<PeriodReportModel> _items;

        public ReportPeriodsAdapter(Activity context, MoneyCalculationModel model)
            : base()
        {
            _context = context;
            _model = model;
            model.Report.CurrentReportPeriodSizeChanged += Report_CurrentReportPeriodSizeChanged;
            _items = model.Report.PeriodReports == null ? new List<PeriodReportModel>() : model.Report.PeriodReports.ToList();
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return _items.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(Resource.Layout.MoneyCalculationPeriodReportLayout, parent, false);
            HookModelToView(view, _items[position]);
            return view;
        }

        private void HookModelToView(View view, PeriodReportModel periodReportModel)
        {
            TextView periodTextView = view.FindViewById<TextView>(Resource.Id.periodTextView);
            periodTextView.Text = string.Format("{0} - {1}", periodReportModel.StartDate, periodReportModel.FinishDate);
        }

        private void Report_CurrentReportPeriodSizeChanged(object sender, EventArgs e)
        {
            _items = _model.Report.PeriodReports.ToList() ?? new List<PeriodReportModel>();
            NotifyDataSetChanged();
        }
    }
}